/**
 * Created by wxy on 2018/1/10.
 */
;(function () {
    angular.module('answer', [])
        .service('AnswerService', [
            '$http',
            '$state',
            function ($http, $state) {
                var me = this;
                me.data = {};
                me.answer_form = {};
                /*
                 * 统计票数
                 * */
                me.count_vote = function (answers) {
                    /*迭代所有的数据*/
                    for (var i = 1; i < answers.length; i++) {
                        /*封装单个数据*/
                        var votes, item = answers[i];
                        /*如果不是回答也没有users元素说明本条不是回答或回答没有任何票数*/
                        if (!item['question_id'])
                            continue;
                        me.data[item.id] = item;
                        if (!item['users'])
                            continue;

                        item.upvote_count = 0;
                        item.downvote_count = 0;
                        //console.log('进来了');
                        votes = item['users'];
                        if (votes) {
                            for (var j = 0; j < votes.length; j++) {
                                var v = votes[j];

                                if (v['pivot'].vote === 1)
                                    item.upvote_count++;
                                if (v['pivot'].vote === 2)
                                    item.downvote_count++;
                            }
                        }

                    }
                    return answers;
                };
                /*
                 *
                 * */
                me.vote = function (conf) {
                    if (!conf.id || !conf.vote) {
                        console.log('id and vote required');
                        return;
                    }

                    var his = {
                        id: parseInt($('body').attr('user-id'))
                    };

                    var answer = me.data[conf.id], users = answer.users;
                    /*判断用户是否已近投过相同的票了  挪到后台判断也行*/
                    for (var i = 0; i < users.length; i++) {
                        if (users[i].id == his.id && conf.vote == users[i].pivot.vote) {
                            conf.vote = 3;
                        }
                    }

                    return $http.post('/laravel54/public/index.php/api/answer/vote', conf)
                        .then(function (r) {
                            //console.log(r);
                            if (r.data.status)
                                return true;
                            //用到的时候打开state
                            else if (r.data.msg == 'login required')
                                $state.go('login');
                            else
                                return false;
                        }, function () {
                            return false;
                        });
                };
                /*
                 *
                 * */
                me.update_data = function (id) {

                    return $http.post('/laravel54/public/index.php/api/answer/read', {id: id})
                        .then(function (r) {
                            me.data[id] = r.data.data;
                        })
                };
                /*
                 *
                 * */
                me.read = function (params) {
                    return $http.post('/laravel54/public/index.php/api/answer/read', params)
                        .then(function (r) {
                            if (r.data.status) {
                                me.data = angular.merge({}, me.data, r.data.data);
                                return r.data.data;
                            }
                            return false;
                        })
                }
                /*
                 *
                 * */
                me.add_or_update = function (question_id) {


                    if (question_id) {
                        me.answer_form.id = question_id;
                        $http.post('/laravel54/public/index.php/api/answer/change', me.answer_form)
                    }
                    else
                        $http.post('/laravel54/public/index.php/api/answer/add', me.answer_form)
                }
                /*
                 *
                 * */
                me.delete = function (id) {
                    $http.post('/laravel54/public/index.php/api/answer/remove', id)
                        .then(function (r) {
                            if(r.data.staus){
                                console.log('deleted successfully!')
                            }
                        })
                }
            }
        ])

})();